package com.sgb.trip.view.widget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sgb.link.entity.trip.FlightSearchEntity;
import com.sgb.trip.R;
import com.sgb.trip.databinding.TripPopwindowOrderAccountDetailBinding;
import com.sgb.trip.entity.FlightInfoEntity;
import com.sgb.trip.entity.FlightOrderDetailEntity;
import com.sgb.trip.entity.InsureServiceEntity;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.util.NumberUtil;

import java.util.List;

public class FlightOrderDetailPopWindow extends BasePopWindow implements View.OnClickListener {
    TripPopwindowOrderAccountDetailBinding mBinding;

    FlightInfoEntity mEntity;

    FlightSearchEntity mEntitySearch;

    FlightInfoEntity mEntityTo;

    int positionTo;

    int positionBack;

    int passengerCount;

    List<InsureServiceEntity> insureArrayList;

    double allCount;

    public FlightOrderDetailPopWindow(Context context) {
        super(context, DIR_DOWN_UP);
        setSoftInputMode
                (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN |
                        WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    }

    @Override
    protected int popLayout() {
        return R.layout.trip_popwindow_order_account_detail;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        mBinding = (TripPopwindowOrderAccountDetailBinding) holder.getmBinding();
        mBinding.ivCancelDetail.setOnClickListener(view -> dismiss());
    }

    public void setEntitySearch(FlightSearchEntity entitySearch) {
        this.mEntitySearch = entitySearch;
    }

    public void setEntityTo(FlightInfoEntity entityTo) {
        this.mEntityTo = entityTo;
    }

    public void setEntityBack(FlightInfoEntity entityBack) {
        this.mEntity = entityBack;
    }

    public void setPositionTo(int positionTo) {
        this.positionTo = positionTo;
    }

    public void setPositionBack(int positionBack) {
        this.positionBack = positionBack;
    }

    public void setPassengerCount(int passengerCount) {
        this.passengerCount = passengerCount;
    }

    public void setInsureArrayList(List<InsureServiceEntity> insureArrayList) {
        this.insureArrayList = insureArrayList;
        mBinding.insureLayout.removeAllViews();

        if (insureArrayList != null && insureArrayList.size() > 0) {
            mBinding.insureLayout.setVisibility(View.VISIBLE);
            for (int i = 0; i < insureArrayList.size(); i++) {
                if (mEntitySearch.isSingle()) {
                    addCustomLayout(mBinding.insureLayout, insureArrayList.get(i), false);
                } else {
                    addCustomLayout(mBinding.insureLayout, insureArrayList.get(i), true);
                }
            }

        }
    }

    public void setAllCount(double allCount) {
        this.allCount = allCount;
    }

    public void setDataShow() {
        if (mEntitySearch != null) {
            if (mEntitySearch.isSingle()) {
                mBinding.rlBackPrice.setVisibility(View.GONE);
                if (mEntityTo != null) {
                    mBinding.tvToHint.setText("成人票");
                    mBinding.tvToTicketPrice.setText("¥" + NumberUtil.trim0(mEntityTo.getTickets().get(positionTo).getCabinPrice().getAdultFarePrice()) + "*" + passengerCount);
                    mBinding.tvTaxPrice.setText("¥" + NumberUtil.trim0(mEntityTo.getAdultTax()) + "*" + passengerCount);
                    mBinding.tvFuelPrice.setText("¥" + NumberUtil.trim0(mEntityTo.getAdultFuel()) + "*" + passengerCount);
                    mBinding.tvAccount.setText("¥" + NumberUtil.formatTwoDigits(allCount));
                }
            } else {
                mBinding.rlBackPrice.setVisibility(View.VISIBLE);
                mBinding.tvToTicketPrice.setText("¥" + NumberUtil.trim0(mEntityTo.getTickets().get(positionTo).getCabinPrice().getAdultFarePrice()) + "*" + passengerCount);
                mBinding.tvBackTicketPrice.setText("¥" + NumberUtil.trim0(mEntity.getTickets().get(positionBack).getCabinPrice().getAdultFarePrice()) + "*" + passengerCount);
                mBinding.tvTaxPrice.setText("¥" + NumberUtil.trim0(mEntityTo.getAdultTax() + mEntity.getAdultTax()) + "*" + passengerCount);
                mBinding.tvFuelPrice.setText("¥" + NumberUtil.trim0(mEntityTo.getAdultFuel() + mEntity.getAdultFuel()) + "*" + passengerCount);
                mBinding.tvAccount.setText("¥" + NumberUtil.formatTwoDigits(allCount));
            }
        }
    }


    /**
     * 为明细添加布局
     *
     * @param content
     * @param entity
     */
    private void addCustomLayout(LinearLayout content, InsureServiceEntity entity, boolean isBack) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.trip_check_order_detail_layout, null);
        TextView name = view.findViewById(R.id.insure_name);
        TextView price = view.findViewById(R.id.insure_price);
        name.setText(entity.getInsuranceName());
        if (isBack) {
            price.setText("¥" + NumberUtil.trim0(entity.getInsuranceFee() * 2) + "*" + passengerCount);
        } else {
            price.setText("¥" + NumberUtil.trim0(entity.getInsuranceFee()) + "*" + passengerCount);
        }
        content.addView(view);
    }

    /**
     * 为明细添加布局
     *
     * @param content
     * @param entity
     */
    private void addCustomLayout(LinearLayout content, FlightOrderDetailEntity.OrderPriceDetailsBean.InsurancePricesBean entity, boolean isBack) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.trip_check_order_detail_layout, null);
        TextView name = view.findViewById(R.id.insure_name);
        TextView price = view.findViewById(R.id.insure_price);
        name.setText(entity.getInsuranceName());
        if (isBack) {
            price.setText("¥" + NumberUtil.trim0(entity.getInsuranceAmount() / passengerCount * 2) + "*" + passengerCount);
        } else {
            price.setText("¥" + NumberUtil.trim0(entity.getInsuranceAmount() / passengerCount) + "*" + passengerCount);
        }
        content.addView(view);
    }

    @Override
    public void onClick(View view) {

    }

    public void setDetailDataShow(FlightOrderDetailEntity entity, String allCount, int passengerCount) {
        this.passengerCount = passengerCount;
        if (entity != null) {
            List<FlightOrderDetailEntity.OrderPriceDetailsBean.InsurancePricesBean> insurancePricesBeanList = entity.getOrderPriceDetails().get(0).getInsurancePrices();
            if (entity.getOrderPriceDetails().size() == 1) {
                mBinding.rlBackPrice.setVisibility(View.GONE);

                mBinding.tvToHint.setText("成人票");
                mBinding.tvToTicketPrice.setText("¥" + NumberUtil.trim0(entity.getOrderPriceDetails().get(0).getTicketPrice() / passengerCount) + "*" + passengerCount);
                mBinding.tvTaxPrice.setText("¥" + NumberUtil.trim0(entity.getOrderPriceDetails().get(0).getAdultTax() / passengerCount) + "*" + passengerCount);
                mBinding.tvFuelPrice.setText("¥" + NumberUtil.trim0(entity.getOrderPriceDetails().get(0).getAdultFuel() / passengerCount) + "*" + passengerCount);
                mBinding.tvAccount.setText("¥" + NumberUtil.formatTwoDigits(entity.getOrderAmount()));
                if (insurancePricesBeanList != null && insurancePricesBeanList.size() > 0) {
                    mBinding.insureLayout.setVisibility(View.VISIBLE);
                    mBinding.insureLayout.removeAllViews();
                    for (int i = 0; i < insurancePricesBeanList.size(); i++) {
                        addCustomLayout(mBinding.insureLayout, insurancePricesBeanList.get(i), false);
                    }
                }
            } else {
                mBinding.rlBackPrice.setVisibility(View.VISIBLE);
                mBinding.tvToTicketPrice.setText("¥" + NumberUtil.trim0(entity.getOrderPriceDetails().get(0).getTicketPrice() / passengerCount) + "*" + passengerCount);
                mBinding.tvBackTicketPrice.setText("¥" + NumberUtil.trim0(entity.getOrderPriceDetails().get(1).getTicketPrice() / passengerCount) + "*" + passengerCount);
                mBinding.tvTaxPrice.setText("¥" + NumberUtil.trim0(entity.getOrderPriceDetails().get(0).getAdultTax() / passengerCount + entity.getOrderPriceDetails().get(1).getAdultTax() / passengerCount) + "*" + passengerCount);
                mBinding.tvFuelPrice.setText("¥" + NumberUtil.trim0(entity.getOrderPriceDetails().get(0).getAdultFuel() / passengerCount + entity.getOrderPriceDetails().get(1).getAdultFuel() / passengerCount) + "*" + passengerCount);
                mBinding.tvAccount.setText("¥" + NumberUtil.formatTwoDigits(entity.getOrderAmount()));
                if (insurancePricesBeanList != null && insurancePricesBeanList.size() > 0) {
                    mBinding.insureLayout.setVisibility(View.VISIBLE);
                    mBinding.insureLayout.removeAllViews();
                    for (int i = 0; i < insurancePricesBeanList.size(); i++) {
                        addCustomLayout(mBinding.insureLayout, insurancePricesBeanList.get(i), true);
                    }
                }
            }

        }
    }
}
